﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.Common;
using Hangjing.SQLServerDAL;
using Hangjing.Model;
using Hangjing.DBUtility;

/// <summary>
///损单统计
/// </summary>
public partial class qy_54tss_Admin_Sale_damageRecordSum : System.Web.UI.Page
{
    /// <summary>
    /// 管理员条件
    /// </summary>
    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? "" : Convert.ToString(o);
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }


    damageRecord bll = new damageRecord();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlWhere = " 1=1 and Custorder.delflag = 0 ";
            SqlWhere = new RightsTool().getOrderWhere(SqlWhere);
           
            WebUtility.SetDDLCity(ddlcity);
            WebUtility.SetDDLGroup(ddlGpsIMEI);

            new OrderState().BindOrderState(rblstate);

            InitSqlwhere();
        }
    }

    /// <summary>
    /// 绑定指定的数据
    /// </summary>
    protected void BindData()
    {
        IList<CustorderInfo> shops = bll.getSumList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SqlWhere, "OrderDateTime", 1);
        this.AspNetPager1.RecordCount = 0;
        if (shops.Count > 0)
        {
            this.AspNetPager1.RecordCount = shops[0].recordtcount;
        }

        this.rptsum.DataSource = shops;
        this.rptsum.DataBind();

        AlertScript.AjaxRegisterScript("init();", UpdatePanel1);
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        InitSqlwhere();
        BindData();
    }

    /// <summary>
    /// 时间变化搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void settime_Click(object sender, EventArgs e)
    {
        DateTime dt = DateTime.Now; //当前时间
        string starttime = this.tbStartTime.Text;
        string endtime = this.tbEndTime.Text;
        Button bt = (Button)sender;
        switch (bt.ID)
        {
            case "btpre"://前一天
                if (starttime != "")
                {
                    tbStartTime.Text = Convert.ToDateTime(starttime).AddDays(-1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                if (endtime != "")
                {
                    tbEndTime.Text = Convert.ToDateTime(endtime).AddDays(-1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                break;
            case "btyestoday"://昨天
                tbStartTime.Text = DateTime.Now.AddDays(-1).ToShortDateString() + "";
                tbEndTime.Text = DateTime.Now.AddDays(-1).ToShortDateString() + "";

                break;
            case "tbtoday"://今天
                tbStartTime.Text = DateTime.Now.ToShortDateString() + "";
                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";

                break;
            case "btnext"://后一天
                if (starttime != "")
                {
                    tbStartTime.Text = Convert.ToDateTime(starttime).AddDays(1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                if (endtime != "")
                {
                    tbEndTime.Text = Convert.ToDateTime(endtime).AddDays(1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                break;
            case "tbweek"://本周

                DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")));
                tbStartTime.Text = startWeek.ToDate();

                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";
                break;
            case "tbmonth"://本月
                tbStartTime.Text = DateTime.Now.ToString("yyyy-MM-01") + "";
                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";
                break;
            case "tbyear"://本年
                tbStartTime.Text = DateTime.Now.ToString("yyyy-01-01") + "";
                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";

                break;
            default:
                break;
        }
        InitSqlwhere();

        BindData();
    }


    /// <summary>
    /// 生成查询条件
    /// </summary>
    protected void InitSqlwhere()
    {
        SqlWhere = " 1=1 and Custorder.delflag = 0 ";
        SqlWhere = new RightsTool().getOrderWhere(SqlWhere);
        
        if (this.tbStartTime.Text != "")
        {
            SqlWhere += " and  [OrderDateTime] > '" + WebUtility.InputText(this.tbStartTime.Text) + "' ";
        }
        if (this.tbEndTime.Text != "")
        {
            SqlWhere += " and OrderDateTime < '" + WebUtility.InputText(this.tbEndTime.Text) + " 23:59:59'";
        }
        string ids = WebUtility.GetcheckStr(rblstate);
        if (ids != "")
        {
            SqlWhere += " and OrderStatus  in (" + ids + ")";
        }
        if (tbshopname.Text != "")
        {
            SqlWhere += " and  Points.Name like '%" + Utils.RegEsc(WebUtility.InputText(this.tbshopname.Text.Trim())) + "%' ";
        }
        if (tbdelivername.Text != "")
        {
            SqlWhere += " and  deliver.Name like '%" + Utils.RegEsc(WebUtility.InputText(this.tbdelivername.Text.Trim())) + "%' ";
        }
        if (ddlGpsIMEI.SelectedValue != "-1")
        {
            SqlWhere += " and  GpsIMEI = " + ddlGpsIMEI.SelectedValue + " ";
        }
        if (ddlcity.SelectedValue != "0")
        {
            SqlWhere += " and Custorder.cityid = " + ddlcity.SelectedValue + "";
        }
        if (ddlresponsibleparty.SelectedValue != "-1")
        {
            SqlWhere += " and responsibleparty = " + ddlresponsibleparty.SelectedValue + "";
        }
        if (tbremark.Text != "")
        {
            SqlWhere += " and remark like '%" + Utils.RegEsc(WebUtility.InputText(this.tbremark.Text.Trim())) + "%' ";
        }




    }


    protected void btExport_Click(object sender, EventArgs e)
    {
        //判断权限
        int _rs = WebUtility.checkOperator_super(7);
        if (_rs == 0)
        {
            AlertScript.RegScript(this.Page, UpdatePanel1, "alert('无操作权限','success','true',5);init();");
            return;
        }
        WebUtility.FixsetCookie("SearchOrderSqlWhere", SqlWhere, 1);
        Response.Redirect("damageRecordToExcel.aspx?type=1");
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }


}
